package com.example.dell.fastshopping.user.UserInfo;

import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel;

import com.example.dell.fastshopping.network.ApiService;
import com.example.dell.fastshopping.network.NetCallback;
import com.example.dell.fastshopping.network.RetrofitClient;
import com.example.dell.fastshopping.network.http.BaseResponse;
import com.example.dell.fastshopping.user.entity.UserInfo;
import com.example.dell.fastshopping.utils.ToastUtils;
import com.google.gson.GsonBuilder;

import java.io.File;
import java.util.HashMap;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by ${YSH} on 2019/5/15.
 * 描述        ${TODO}
 * <p>
 * 更新者       $author$
 * 更新时间     $Data$
 * 描述         ${TODO}
 */
public class UserViewModle extends ViewModel {
    private ApiService service = RetrofitClient.getInstance().create(ApiService.class);
    private MutableLiveData<UserInfo> userInfo = new MutableLiveData();
    private MutableLiveData<UserInfo> baseUserInfo = new MutableLiveData();
    private MutableLiveData<BaseResponse> name = new MutableLiveData();
    private MutableLiveData<BaseResponse> icon = new MutableLiveData();

    public MutableLiveData<UserInfo> getUserInfo() {

        RetrofitClient.execute(service.getUserInfo(), new NetCallback<BaseResponse<UserInfo>>() {
            @Override
            public void netCallback(BaseResponse<UserInfo> o) {
                if (o.isSuccess()) {
                    userInfo.setValue(o.getData());
                } else {
                    ToastUtils.showShort(o.getMsg());
                }
            }
        });
        return userInfo;
    }


    public MutableLiveData<UserInfo> getUserBaseInfo() {
        RetrofitClient.execute(service.getUserBaseInfo(), new NetCallback<BaseResponse<UserInfo>>() {
            @Override
            public void netCallback(BaseResponse<UserInfo> o) {
                if (o.isSuccess()) {
                    baseUserInfo.setValue(o.getData());
                } else {
                    ToastUtils.showShort(o.getMsg());
                }
            }
        });
        return baseUserInfo;
    }

    public MutableLiveData<BaseResponse> editUser(UserInfo userInfo) {
//        更新用户  用户名,性别等等基础信息 post
//        接口: /user/updateUserInfo
//        参数: phone手机  sex性别 0女1男 userName用户名
        String s = new GsonBuilder().create().toJson(userInfo);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), s);
        RetrofitClient.execute(service.edituser(body), new NetCallback<BaseResponse>() {
            @Override
            public void netCallback(BaseResponse o) {
                if (o.isSuccess()) {
                    name.setValue(o);
                } else {
                    ToastUtils.showShort(o.getMsg());
                }
            }
        });
        return name;
    }


    public LiveData<BaseResponse> updateIcon(File file) {


        //构建body
        RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("image/*"), file))
                .build();

        RetrofitClient.execute(service.updateIcon(body), new NetCallback<BaseResponse>() {
            @Override
            public void netCallback(BaseResponse o) {
                if (o.isSuccess()) {
                    icon.setValue(o);
                } else {
                    ToastUtils.showShort(o.getMsg());
                }
            }
        });

        return icon;
    }
}
